<?php
session_start();
$domain = $_POST['domain'];
if ($domain=="")
  unset($domain);
mysql_connect("localhost", "", "");
mysql_query("USE stats_syslog");

$curdate = date("o-m-d");
if (date("G")<6 | (date("G"==6) & date("i")<30))
  $timequer = "timestamp>DATE_ADD(\"$curdate 06:25:00\",INTERVAL -2 DAY)";
else
  $timequer = "timestamp>DATE_ADD(\"$curdate 06:25:00\",INTERVAL -1 DAY)";
  
if (isset($domain))
  $domquer = "AND Domain='$domain'";
else
  $domquer = "";

$virusquery = mysql_query("SELECT ReasonExt, COUNT(*) AS count FROM maillog WHERE Reason = 'Virus' AND $timequer $domquer GROUP BY ReasonExt ORDER BY count DESC LIMIT 0,10");
//$blacklistsquery = mysql_query("SELECT ReasonExt, COUNT(*) AS count FROM maillog WHERE Action = 'Per-User Quarantine' AND Reason = 'Blacklist' GROUP BY ReasonExt ORDER BY count DESC LIMIT 0,10");
$blacklistsquery = mysql_query("SELECT ReasonExt, COUNT(*) AS count FROM maillog WHERE Reason = 'Blacklist' AND $timequer $domquer GROUP BY ReasonExt ORDER BY count DESC LIMIT 0,10");
//$recipientsquery = mysql_query("SELECT Recipient, COUNT(*) AS count FROM maillog WHERE Action = 'Allowed' $domquer GROUP BY Recipient ORDER BY count DESC LIMIT 0,10");
$recipientsquery = mysql_query("SELECT Recipient, COUNT(*) AS count FROM maillog WHERE $timequer $domquer GROUP BY Recipient ORDER BY count DESC LIMIT 0,10");
$actionquery = mysql_query("SELECT Action , COUNT( * ) AS count FROM maillog WHERE $timequer $domquer GROUP BY Action ORDER BY count DESC LIMIT 0 , 10");
$reasonquery = mysql_query("SELECT Reason , COUNT( * ) AS count FROM maillog WHERE $timequer $domquer GROUP BY Reason ORDER BY count DESC LIMIT 0 , 10");
$domainquery = mysql_query("SELECT Domain , COUNT( * ) AS count FROM maillog WHERE $timequer GROUP BY Domain ORDER BY count DESC LIMIT 0 , 10");
$domainsquery = mysql_query("SELECT domain FROM domains");
$mintimequery = mysql_query("SELECT MIN(timestamp) AS mintime FROM maillog WHERE $timequer");
$mintime = mysql_result($mintimequery,0,mintime);
$maxtimequery = mysql_query("SELECT MAX(timestamp) AS maxtime FROM maillog");
$maxtime = mysql_result($maxtimequery,0,maxtime);

while($row_virus = mysql_fetch_row($virusquery)) {
      $virus_names_temp[] = $row_virus;
}
while($row_blacklist = mysql_fetch_row($blacklistsquery)) {
      $blacklist_names_temp[] = $row_blacklist;
}
while($row_recipient = mysql_fetch_row($recipientsquery)) {
      $recipient_names_temp[] = $row_recipient;
}
while($row_action = mysql_fetch_row($actionquery)) {
      $action_names_temp[] = $row_action;
}
while($row_reason = mysql_fetch_row($reasonquery)) {
      $reason_names_temp[] = $row_reason;
}
while($row_domain = mysql_fetch_row($domainquery)) {
      $domain_names_temp[] = $row_domain;
}
mysql_close();
if (mysql_num_rows($virusquery)) {
 foreach ($virus_names_temp as $key => $row_virus) {
		  $virus_names[$key] = $row_virus[0];
 }
 foreach ($virus_names_temp as $key => $row_virus) {
		  $virus_values[$key] = $row_virus[1];
 }
}
if (mysql_num_rows($blacklistsquery)) {
 foreach ($blacklist_names_temp as $key => $row_blacklist) {
		  $blacklist_names[$key] = $row_blacklist[0];
 }
 foreach ($blacklist_names_temp as $key => $row_blacklist) {
		  $blacklist_values[$key] = $row_blacklist[1];
 }
}
if (mysql_num_rows($recipientsquery)) {
 foreach ($recipient_names_temp as $key => $row_recipient) {
		  $recipient_names[$key] = $row_recipient[0];
 }
 foreach ($recipient_names_temp as $key => $row_recipient) {
		  $recipient_values[$key] = $row_recipient[1];
 }
}
if (mysql_num_rows($actionquery)) {
 foreach ($action_names_temp as $key => $row_action) {
		  $action_names[$key] = $row_action[0];
 }
 foreach ($action_names_temp as $key => $row_action) {
		  $action_values[$key] = $row_action[1];
 }
}
if (mysql_num_rows($reasonquery)) {
 foreach ($reason_names_temp as $key => $row_reason) {
		  $reason_names[$key] = $row_reason[0];
 }
 foreach ($reason_names_temp as $key => $row_reason) {
		  $reason_values[$key] = $row_reason[1];
 }
}
if (mysql_num_rows($domainquery)) {
 foreach ($domain_names_temp as $key => $row_domain) {
		  $domain_names[$key] = $row_domain[0];
 }
 foreach ($domain_names_temp as $key => $row_domain) {
		  $domain_values[$key] = $row_domain[1];
 }
}
$_SESSION['virus_names'] = $virus_names;
$_SESSION['virus_values'] = $virus_values;
$_SESSION['blacklist_names'] = $blacklist_names;
$_SESSION['blacklist_values'] = $blacklist_values;
$_SESSION['recipient_names'] = $recipient_names;
$_SESSION['recipient_values'] = $recipient_values;
$_SESSION['action_names'] = $action_names;
$_SESSION['action_values'] = $action_values;
$_SESSION['reason_names'] = $reason_names;
$_SESSION['reason_values'] = $reason_values;
$_SESSION['domain_names'] = $domain_names;
$_SESSION['domain_values'] = $domain_values;
echo "<html>\n";
echo "<head>\n";
if (isset($domain))
  echo "<title>Barracuda Spam Firewall One-Day Statistics - Domain '$domain'</title>\n";
else
  echo "<title>Barracuda Spam Firewall One-Day Statistics - All Domains</title>\n";
echo "</head>\n";
echo "<body>\n";
if (isset($domain))
  echo "<b>Firewall One-Day Statistics - Domain '$domain'</b>\n";
else
  echo "<b>Firewall One-Day Statistics - All Domains</b>\n";
echo "<BR><BR><b>Statistics Range:</b> <b>$mintime</b> through <b>$maxtime</b><BR><BR>";

//POP-UP BOX
echo "<FORM method=post action=stats_1day.php>";
echo "<SELECT NAME=domain onChange=\"submit()\">";
echo "<OPTION";
if (!isset($domain))
  echo " SELECTED";
echo " VALUE=\"\">All Domains";
$domaincount = mysql_num_rows($domainsquery);
for ($domainnum=0; $domainnum<$domaincount; $domainnum++)
{
  $currdomain = mysql_result($domainsquery,$domainnum,domain);
  echo "<OPTION";
  if ($currdomain==$domain)
    echo " SELECTED";
  echo ">$currdomain";
}
echo "</SELECT>";
echo "&nbsp;<a href=\"domain_edit.php\" target=\"_blank\"><b>Edit List of Domains</b></a>\n";
echo "</FORM>";
//END POP-UP BOX

echo "<br>\n";
echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td><b>Top 10 Viruses</b></td>\n";
echo "<td><b>Top DNS-Blacklists</b></td>\n";
echo "<td><b>Top 10 Recipients</b></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align=center valign=top><table border=1><tr><td>Virus</td><td>Number</td></tr>";
if (mysql_num_rows($virusquery)) {
  foreach ($virus_names as $i =>$name) {
    $number = $virus_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($virusquery))
// echo "<td><img src=\"topvirus-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "<td align=center valign=top><table border=1><tr><td>Blacklist</td><td>Number</td></tr>";
if (mysql_num_rows($blacklistsquery)) {
  foreach ($blacklist_names as $i =>$name) {
    $number = $blacklist_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($blacklistsquery))
// echo "<td><img src=\"topblacklists-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "<td align=center valign=top><table border=1><tr><td>Recipient</td><td>Number</td></tr>";
if (mysql_num_rows($recipientsquery)) {
  foreach ($recipient_names as $i =>$name) {
    $number = $recipient_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($recipientsquery))
// echo "<td><img src=\"toprecipients-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><b>Top Actions</b></td>\n";
echo "<td><b>Top Reasons</b></td>\n";
echo "<td><b>Top Domains</b></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align=center valign=top><table border=1><tr><td>Action</td><td>Number</td></tr>";
if (mysql_num_rows($actionquery)) {
  foreach ($action_names as $i =>$name) {
    $number = $action_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($actionquery))
// echo "<td><img src=\"topaction-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "<td align=center valign=top><table border=1><tr><td>Reason</td><td>Number</td></tr>";
if (mysql_num_rows($reasonquery)) {
  foreach ($reason_names as $i =>$name) {
    $number = $reason_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($reasonquery))
// echo "<td><img src=\"topreason-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "<td align=center valign=top><table border=1><tr><td>Domain</td><td>Number</td></tr>";
if (mysql_num_rows($domainquery)) {
  foreach ($domain_names as $i =>$name) {
    $number = $domain_values[$i];
    echo "<tr><td>$name</td><td>$number</td></tr>";
  }
}
else
  echo "<tr><td colspan=2 align=center>NONE</td></tr>";
echo "</table></td>";
//if (mysql_num_rows($domainquery))
// echo "<td><img src=\"topdomain-diagramm-global.php\" border=\"0\"></td>\n";
//else
// echo "<td><center>NONE</center></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br>\n";
//echo "<a href=\"per-domain.php\" target=\"_blank\"><b>Per-Domain Statistics - Overview</b></a>\n";
echo "<a href=\"stats.php\"><b>Original Stats</b></a>\n";
echo "</body>\n";
echo "</html>\n";
?>
